<template>
  <div class="upload-body">
    <el-dialog v-model="centerDialogVisible" title="所有话题" width="30%" center>
      <div class="search-box f-j-sb-a-c">
        <div class="search-input f-a-c">
          <el-input v-model="searchValue" placeholder="请输入您想添加的话题"></el-input>
        </div>

        <div class="circle-btn add-circle-btn f-j-a-c" @click="postTagsAdd">
          <span>添加话题</span>
        </div>
      </div>

      <!-- 话题标签列表 -->
      <div class="topic-tag-list flex">
        <div class="f-j-a-c" v-for="(item,index) in selectTopicList" :key="index">
          <span># {{item.tags_name}}</span>
          <svg-icon class="svg-icon" name="icon-tsc-lv" @click="removeSelectTopic(item,index)"></svg-icon>
        </div>
      </div>

      <!-- 标题 -->
      <div class="topic-title">推荐话题</div>

      <!-- 话题列表 -->
      <div class="topic-list">
        <div class="topic-item f-j-sb-a-c" v-for="(item,index) in topicList" :key="index" @click="addSelectTopic(item)">
          <div class="topic-item-info f-a-c">
            <img :src="item.head_portrait" alt="" />
            <span>#{{item.tags_name}}</span>
          </div>
          <span class="topic-num">{{item.tags_number}}条笔记</span>
        </div>

        <!-- 暂无数据 -->
        <noData v-if="!topicList.length"></noData>
      </div>


      <template #footer right>
      <div class="dialog-footer f-j-a-c">
        <div class="f-j-a-c" @click="confirm">确定</div>
      </div>
       
      </template>

    </el-dialog>
  </div>
</template>
<script>
import { recommendSearch,tagsAdd } from "@/axios/community.js";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
  defineComponent,
  reactive,
  toRefs,
  onUnmounted,
  onMounted,
  computed,
  getCurrentInstance,
  watch,
  ref,
} from "vue";
import createCircle from "./createCircle.vue";
import { ElMessage } from "element-plus";
import {textTrim} from "@/utils/regTest.js";

export default defineComponent({
  components: {
    createCircle,
  },
  props:{
    topic:{
      type:[String,Array],
      default:""
    }
  },
  setup(props, { attrs, slots, emit }) {
    const router = useRouter();
    const { proxy } = getCurrentInstance();
    const store = useStore();


    let data = reactive({
      centerDialogVisible: false,
      topicList: [], //圈子列表
      searchValue: "", // 输入框内容
      searchTimer: null, //搜索定时器
      selectTopicList:[], //选中圈子
    });

    // 监听弹框打开隐藏
    watch(
      () => data.centerDialogVisible,
      (newVal, oldVal) => {
        if (newVal) {
          getRecommendSearch();
        }
      }
    );

    // 监听输入
    watch(
      () => data.searchValue,
      (newVal, oldVal) => {
        clearTimeout(data.searchTimer);
        data.searchTimer = setTimeout(() => {
            getRecommendSearch();
        }, 500);
      }
    );



    // 搜索圈子
    let getRecommendSearch = () => {
        recommendSearch({
        keyword: data.searchValue,
      }).then((res) => {
        let resultData = res.data;
        data.topicList = resultData?resultData:[];
      });
    };


    // 话题列表点击
    let addSelectTopic = (item) =>{
      let filterList = data.selectTopicList.filter(v=>v.id == item.id);
      if(filterList.length) return ElMessage.warning("您已添加过该话题");
      data.selectTopicList.push(item);
      data.searchValue = "";
    };

    // 添加话题
    let postTagsAdd = ()=>{
      if(!data.searchValue || !textTrim(data.searchValue)) return ElMessage.warning("话题标题不能为空!")

      tagsAdd({
        tags_name:data.searchValue
      }).then(res=>{
        addSelectTopic(res.data);
      })
    };

    // 移除选中话题
    let removeSelectTopic = (item,index)=>{
      data.selectTopicList.splice(index,1)
    };



    // 打开弹框
    let open = () => {
      if(props.topic) {
        data.selectTopicList = props.topic;
      };
      data.centerDialogVisible = true;
    };

    // 取消
    let close = () => {
      data.selectTopicList = [];
      data.searchValue = "";
      data.centerDialogVisible = false;
    };

    // 确定
    let confirm = () => {
      let topic = data.selectTopicList.map(v=>{
        return {
          id:v.id,
          tags_name:v.tags_name
        }
      })
      emit("confirm",topic);
      close();
    };

    return {
      removeSelectTopic,
      postTagsAdd,
      addSelectTopic,
      close,
      confirm,
      open,
      ...toRefs(data),
    };
  },
});
</script>
<style lang="scss" scoped>
.upload-body {
  :deep(.el-dialog.el-dialog--center) {
    border-radius: 20px;
    min-width: 520px;
    max-width: 600px;
  }

  :deep(.el-dialog--center .el-dialog__footer) {
    text-align: right !important;
  }
}

// 按钮公用样式
.circle-btn {
  font-size: 14px;
  color: #64c8dc;
  border: 1px solid #64c8dc;
  border-radius: 10px;
  width: 84px;
  height: 28px;
  cursor: pointer;
  &:active {
    opacity: 0.7;
  }
  .svg-icon {
    width: 24px;
    height: 24px;
    margin-right: 5px;
  }
}

.search-box {
  padding-bottom: 10px;
  .search-input {
    flex: 1;
    :deep(.el-input__wrapper) {
      background: #f5f6f7;
      border: none;
      height: 36px;
      border-radius: 10px;
      box-shadow: none;
    }
    :deep(.el-input__wrapper.is-focus) {
      box-shadow: 0 0 0 1px #64c8dc inset;
    }
  }

  .circle-btn {
    height: 36px;
    margin-left: 10px;
  }
}

// 话题标签列表
.topic-tag-list {
  flex-wrap: wrap;
  > div {
    margin-top: 10px;
    position: relative;
    margin-right: 20px;
    height: 30px;
    border-radius: 15px;
    border: 1px solid #ececec;
    padding: 0 15px;
    font-size: 12px;
    color: #000000;
    .svg-icon {
      position: absolute;
      width: 16px;
      height: 16px;
      right: -6px;
      top: -8px;
      cursor: pointer;
    }
  }
}

// 标题
.topic-title {
  padding-top: 20px;
  font-size: 14px;
  color: #64c8dc;
  font-weight: bold;
  border-bottom: 1px solid #f5f6f7;
  padding-bottom: 10px;
}

// 列表
.topic-list {
  max-height: 360px;
  overflow-y: auto;
  .topic-item {
    margin-top: 20px;
    cursor: pointer;
    .topic-item-info {
      > img {
        width: 60px;
        height: 60px;
        border-radius: 10px;
      }
      > span {
        font-size: 14px;
        color: #000000;
        font-weight: bold;
        padding-left: 20px;
      }
    }

    .topic-num{
        color: #656F79;
        font-size: 14px;
    }
  }
}

// 底部按钮
.dialog-footer {
  font-size: 14px;
  font-weight: bold;
  border-top: 1px solid #F5F6F7;
  margin-top: -20px;
  padding-top: 15px;
  > div {
    height: 40px;
    width: 260px;
    border-radius: 20px;
    cursor: pointer;
    background: linear-gradient(90deg, #9ed8e6 0%, #a1f0c0 100%);
    color: #ffffff;
    &:active {
      opacity: 0.7;
    }
  }
}
</style>
